import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Query,
  UseGuards,
} from "@nestjs/common";
import { CasinoService } from "./casino.service";
import { GameType } from "./entities";

@Controller("casino")
export class CasinoController {
  constructor(private readonly casinoService: CasinoService) {}

  // ==================== 大小玩法接口 ====================

  /**
   * 获取大小游戏信息
   */
  @Get("dice/info")
  async getDiceGameInfo() {
    return await this.casinoService.getDiceGameInfo();
  }

  /**
   * 大小玩法下注
   */
  @Post("dice/bet")
  async placeDiceBet(@Body() betData: any) {
    // TODO: 从认证中获取用户ID
    const userId = 1; // 临时硬编码
    return await this.casinoService.placeDiceBet(userId, betData);
  }

  /**
   * 获取大小历史记录
   */
  @Get("dice/history")
  async getDiceHistory(@Query("page") page: string = "0") {
    const pageNum = parseInt(page);
    return await this.casinoService.getDiceHistory(pageNum);
  }

  /**
   * 获取大小我的投注记录
   */
  @Get("dice/my-bets")
  async getDiceMyBets(
    @Query("page") page: string = "0",
    @Query("userId") userId: string = "1"
  ) {
    const pageNum = parseInt(page);
    const userIdNum = parseInt(userId);
    return await this.casinoService.getDiceMyBets(userIdNum, pageNum);
  }

  /**
   * 获取大小排行榜
   */
  @Get("dice/rankings")
  async getDiceRankings(@Query("page") page: string = "0") {
    const pageNum = parseInt(page);
    return await this.casinoService.getRankings(GameType.DICE, "win", pageNum);
  }

  // ==================== 赤壁玩法接口 ====================

  /**
   * 获取赤壁游戏信息
   */
  @Get("chibi/info")
  async getChibiGameInfo() {
    return await this.casinoService.getChibiGameInfo();
  }

  /**
   * 赤壁玩法下注
   */
  @Post("chibi/bet")
  async placeChibiBet(@Body() betData: any) {
    // TODO: 从认证中获取用户ID
    const userId = 1; // 临时硬编码
    return await this.casinoService.placeChibiBet(userId, betData);
  }

  /**
   * 获取赤壁中奖名单
   */
  @Get("chibi/winners")
  async getChibiWinners(@Query("page") page: string = "0") {
    const pageNum = parseInt(page);
    return await this.casinoService.getChibiWinners(pageNum);
  }

  /**
   * 获取赤壁历史记录
   */
  @Get("chibi/history")
  async getChibiHistory(@Query("page") page: string = "0") {
    const pageNum = parseInt(page);
    return await this.casinoService.getChibiHistory(pageNum);
  }

  /**
   * 获取赤壁我的支援记录
   */
  @Get("chibi/my-bets")
  async getChibiMyBets(
    @Query("page") page: string = "0",
    @Query("userId") userId: string = "1"
  ) {
    const pageNum = parseInt(page);
    const userIdNum = parseInt(userId);
    return await this.casinoService.getChibiMyBets(userIdNum, pageNum);
  }

  // ==================== 美人计玩法接口 ====================

  /**
   * 发起猜拳挑战
   */
  @Post("rps/challenge")
  async createRockPaperScissors(@Body() challengeData: any) {
    // TODO: 从认证中获取用户ID
    const userId = 1; // 临时硬编码
    return await this.casinoService.createRockPaperScissors(
      userId,
      challengeData
    );
  }

  /**
   * 获取猜拳挑战列表
   */
  @Get("rps/challenges")
  async getRPSChallenges(@Query("page") page: string = "0") {
    const pageNum = parseInt(page);
    return await this.casinoService.getRPSChallenges(pageNum);
  }

  /**
   * 应战猜拳
   */
  @Post("rps/respond")
  async respondToChallenge(@Body() responseData: any) {
    const userId = responseData?.userId || 1;
    const challengeId = responseData?.challengeId;
    const response = responseData?.response;
    const responderName = responseData?.responderName || "用户";

    if (!challengeId) {
      throw new Error("挑战ID不能为空");
    }
    if (!response) {
      throw new Error("响应不能为空");
    }

    const challengeResponseData = {
      challengeId: Number(challengeId),
      responderId: userId,
      responderName: responderName,
      responderValue: Number(response),
    };

    return await this.casinoService.respondToChallenge(
      userId,
      challengeId,
      challengeResponseData
    );
  }

  /**
   * 获取猜拳挑战记录
   */
  @Get("rps/challenge-records")
  async getRPSChallengeRecords(
    @Query("page") page: string = "0",
    @Query("userId") userId: string = "1"
  ) {
    const pageNum = parseInt(page);
    const userIdNum = parseInt(userId);
    return await this.casinoService.getRPSChallengeRecords(userIdNum, pageNum);
  }

  /**
   * 获取猜拳应战记录
   */
  @Get("rps/response-records")
  async getRPSResponseRecords(
    @Query("page") page: string = "0",
    @Query("userId") userId: string = "1"
  ) {
    const pageNum = parseInt(page);
    const userIdNum = parseInt(userId);
    return await this.casinoService.getRPSResponseRecords(userIdNum, pageNum);
  }

  /**
   * 获取猜拳赢家排行
   */
  @Get("rps/win-rankings")
  async getRPSWinRankings(@Query("page") page: string = "0") {
    const pageNum = parseInt(page);
    return await this.casinoService.getRPSWinRankings(pageNum);
  }

  /**
   * 获取猜拳赚币排行
   */
  @Get("rps/profit-rankings")
  async getRPSProfitRankings(@Query("page") page: string = "0") {
    const pageNum = parseInt(page);
    return await this.casinoService.getRPSProfitRankings(pageNum);
  }

  // ==================== 通用接口 ====================

  /**
   * 获取排行榜
   */
  @Get("rankings/:gameType")
  async getRankings(
    @Param("gameType") gameType: string,
    @Query("type") type: string = "win",
    @Query("page") page: string = "0"
  ) {
    const gameTypeNum = parseInt(gameType);
    const pageNum = parseInt(page);
    return await this.casinoService.getRankings(
      gameTypeNum,
      type as "win" | "profit",
      pageNum
    );
  }

  /**
   * 获取系统统计信息
   */
  @Get("statistics")
  async getSystemStatistics() {
    return await this.casinoService.getSystemStatistics();
  }

  /**
   * 手动创建期数（管理用）
   */
  @Post("admin/create-round")
  async createRoundManually(@Body() data: { gameType: number }) {
    return await this.casinoService.createRoundManually(data.gameType);
  }

  /**
   * 手动结算期数（管理用）
   */
  @Post("admin/settle-round/:roundId")
  async settleRoundManually(@Param("roundId") roundId: string) {
    const roundIdNum = parseInt(roundId);
    return await this.casinoService.settleRoundManually(roundIdNum);
  }
}
